package com.example.company_project_fangyu.demo.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.company_project_fangyu.demo.common.LoginSet;
import com.example.company_project_fangyu.demo.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static com.example.company_project_fangyu.demo.common.BasicData.BASIC_URL;

@Service
public class IpcService {
    @Autowired
    HttpClientUtils httpClientUtils;
    @Autowired
    LoginSet loginSet;

    /**
     * 添加IPC设备
     * @param name
     * @param protocol 协议 rtsp:rtsp协议
     * @param rtsp  rtsp协议内容
     * name,protocol，rtsp必选
     * @return
     */
    public JSON addIpc(String name, String protocol, String rtsp,String deviceId,String position,String desc) {
        JSONObject params = new JSONObject();
        params.put("name", name);
        params.put("protocol", protocol);
        params.put("rtsp", rtsp);
        params.put("deviceId", deviceId);
        params.put("position", position);
        params.put("desc", desc);
        String url = BASIC_URL + "/api/open/ipc/add";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 更新IPC设备
     * id,name,protocol，rtsp必选
     */
    public JSON updateIpc(String id,String name, String protocol, String rtsp,String deviceId,String position,String desc) {
        JSONObject params = new JSONObject();
        params.put("id", id);
        params.put("name", name);
        params.put("protocol", protocol);
        params.put("rtsp", rtsp);
        params.put("deviceId", deviceId);
        params.put("position", position);
        params.put("desc", desc);
        String url = BASIC_URL + "/api/open/ipc/update";
        String res = null;
        try {
            res = httpClientUtils.doPutJson(url, params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 根据设备id删除IPC设备
     */
    public JSON deleteIpc(String ipcId) {
        String url = BASIC_URL + "/api/open/ipc/" + ipcId;
        String res = null;
        try {
            res = httpClientUtils.doDelete(url,loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     * 根据设备id获取IPC设备信息
     */
    public JSON getIpcInfo(String ipcId) {
        String url = BASIC_URL + "/api/open/ipc/" + ipcId;
        String res = null;
        try {
            res = httpClientUtils.doGet(url,null,loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

    /**
     *获取IPC设备列表
     */
    public JSON getIpcList(Integer page, Integer pageSize,String name,String ip) {
        JSONObject params = new JSONObject();
        params.put("pageIndex", page.toString());
        params.put("pageSize", pageSize.toString());
        params.put("name", name);
        params.put("ip", ip);
        String url = BASIC_URL + "/api/open/ipc/listDevice";
        String res = null;
        try {
            res = httpClientUtils.doPostJson(url,params.toString(),loginSet.getHeaders());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSON.parseObject(res);
    }

}
